package com.lanou3g.model;

import com.lanou3g.pojo.User;
import com.lanou3g.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class LoginModel {
    public User login(String username, String password) {
            try {
                User user = new User();
                Connection conn = JDBCUtil.getConn();
                // 查询数据库
                String sql = "SELECT * FROM user " +
                        "WHERE username = ? AND pwd = ?";
                PreparedStatement statement
                        = conn.prepareStatement(sql);
                statement.setString(1, username);
                statement.setString(2, password);
                ResultSet rs = statement.executeQuery();
                rs.next();
                // 设置User属性
                String gender = rs.getString("gender");
                String nickname = rs.getString("nickname");
                int id = rs.getInt("uid");
                // 爱好
                //链式编程
                List<String> hobbies = getHobbies(id);
                user.setUsername(username)
                        .setGender(gender)
                        .setNickname(nickname)
                        .setHobbies(hobbies)
                        .setPassword("");
                rs.close();
                statement.close();
                conn.close();
                return user;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }

        private List<String> getHobbies(int id) {
            List<String> hobbies = new ArrayList<>();
            String sql = "SELECT opt FROM user_hobby,hobby " +
                    "WHERE uid = ? AND user_hobby.hid = hobby.hid";
            Connection conn = JDBCUtil.getConn();
            try {
                PreparedStatement statement = conn
                        .prepareStatement(sql);
                statement.setInt(1, id);
                ResultSet rs = statement.executeQuery();
                while (rs.next()) {
                    String hobby = rs.getString(1);
                    hobbies.add(hobby);
                }
                rs.close();
                statement.close();
                conn.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return hobbies;
        }

        //能否登录成功
        public boolean canLogin(String username, String pwd) {
            Connection conn = JDBCUtil.getConn();
            String sql = "SELECT count(*) FROM user WHERE " +
                    "username = ? AND pwd = ?";
            try {
                PreparedStatement statement
                        = conn.prepareStatement(sql);
                statement.setString(1, username);
                statement.setString(2, pwd);
                ResultSet rs = statement.executeQuery();
                boolean canLogin = false;
                if (rs.next()) {
                    int count = rs.getInt(1);
                    canLogin = count > 0;
                }
                statement.close();
                rs.close();
                conn.close();
                return canLogin;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

    }

